<?php

class Utility {
	public static function getRemoteIp($is_strip=false, $default='') {
		if(defined('STRIP_INNER_IP'))
			$is_strip = STRIP_INNER_IP;
		$ip = explode(',', getenv('HTTP_X_FORWARDED_FOR').','.getenv('REMOTE_ADDR').','.getenv('HTTP_CLIENT_IP'));
		foreach($ip as $v)
		{
			if($is_strip && in_array(substr($v, 0, 3), array('10.','127','172','192','224','010')))
				continue;
			if (!in_array($v, array('58.252.73.143', '59.151.107.14')) && preg_match ('/\d+\.\d+\.\d+\.\d+/', $v, $matches) )
				return $matches[0];
		}
		if($is_strip)
			return $default;
		else
			return '127.0.0.1';
	}

	public static function relocation($url) {
		header("location: $url");
		exit;
	}

	public static function checkSpecialApp($method) {
		global $SPECIAL_APPS, $current_app_id;
		if(in_array($current_app_id, $SPECIAL_APPS[$method])) {
			return true;
		}
		return false;
	}
	
	/**
	 * 字节格式化表示(B,K,M,G,T,P)
	 */
	static public function  byte_format($size, $dec=2) {
		$format = array("B", "KB", "MB", "GB", "TB", "PB");
		$pos = 0;
		while ($size >= 1024) {
			 $size /= 1024;
			 $pos++;
		}
		return round($size,$dec)." ".$format[$pos];
	}
}
?>
